package com.reader.cms.service.impl.magazine;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.reader.cms.entity.common.ReaderMagazineColumnRelation;
import com.reader.cms.entity.magazine.ReaderMagazine;
import com.reader.cms.mapper.common.ReaderMagazineColumnRelationMapper;
import com.reader.cms.mapper.magazine.ReaderMagazineMapper;
import com.reader.cms.service.magazine.IReaderMagazineService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @@description  服务实现类
 * @author        gumenghao
 * @date          2018-10-23
 */
@Service
@Transactional
public class ReaderMagazineServiceImpl extends ServiceImpl<ReaderMagazineMapper, ReaderMagazine> implements IReaderMagazineService {


@Autowired
private ReaderMagazineMapper readerMagazineMapper;


@Autowired
private ReaderMagazineColumnRelationMapper readerMagazineColumnRelationMapper;
/**
 * @function 多表关联查询
 * @param     currentPage 当前页
 * @param     size 每页显示条数
 * @return
 */
    @Transactional(readOnly=true)
    @Override
    public Page<ReaderMagazine> selectListPage(int currentPage,int size,String state,Integer year, String period) {
            Page<ReaderMagazine> page = new Page<>(currentPage, size);// 当前页，总条数 构造 page 对象
            return page.setRecords(readerMagazineMapper.selectListPage(page,state,year,period));
    }

    @Override
    public boolean addMagazine(ReaderMagazine readerMagazine) {
        boolean resultValue = this.insert(readerMagazine);
        if(resultValue){
            List<Integer> columnIds = readerMagazine.getColumnIds();
            List<ReaderMagazineColumnRelation> relations = new ArrayList<>();
            for (int i = 0; i < columnIds.size(); i++) {
                ReaderMagazineColumnRelation relation = new ReaderMagazineColumnRelation();
                relation.setMagazineId(readerMagazine.getId());
                relation.setColumnId(columnIds.get(i));
                relation.setInsertDt(new Date());
                relation.setNum(i+1);
                relations.add(relation);
            }
            readerMagazineColumnRelationMapper.insertBaths(relations);
        }
        return resultValue;
    }

    @Override
    public boolean updateMagazine(ReaderMagazine readerMagazine) {
        boolean resultValue = this.updateById(readerMagazine);
        if(resultValue){
            EntityWrapper<ReaderMagazineColumnRelation> wrapper = new EntityWrapper<>();
            wrapper.where("magazine_id = {0}",readerMagazine.getId());
            Integer delete = readerMagazineColumnRelationMapper.delete(wrapper);
            List<Integer> columnIds = readerMagazine.getColumnIds();
            List<ReaderMagazineColumnRelation> relations = new ArrayList<>();
            for (int i = 0; i < columnIds.size(); i++) {
                ReaderMagazineColumnRelation relation = new ReaderMagazineColumnRelation();
                relation.setMagazineId(readerMagazine.getId());
                relation.setColumnId(columnIds.get(i));
                relation.setInsertDt(new Date());
                relation.setNum(i+1);
                relations.add(relation);
            }
            readerMagazineColumnRelationMapper.insertBaths(relations);
        }

        return resultValue;
    }

    @Override
    public ReaderMagazine selectOneMagazine(ReaderMagazine readerMagazine) {
        ReaderMagazine magazine = readerMagazineMapper.selectOne(readerMagazine);
        if(magazine != null){
            EntityWrapper<ReaderMagazineColumnRelation> wrapper = new EntityWrapper<>();
            wrapper.where("magazine_id = {0}",magazine.getId());
            List<ReaderMagazineColumnRelation> relations = readerMagazineColumnRelationMapper.selectList(wrapper);
            List<Integer> columnIds = new ArrayList<>();
            relations.forEach(relation -> {
                columnIds.add(relation.getColumnId());
            });
            magazine.setColumnIds(columnIds);

        }
        return magazine;
    }

}